// pages/openvip/openvip.js
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHide: true,
    isMove: true,
    curHdIndex: 0,
    currentBut: 0,
    type: "wx_pay",
    shareid: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var shareid = ""
    if (options.shareid == undefined) {
      shareid = ""
    } else {
      shareid = options.shareid
    }
    console.log(shareid);
    that.setData({
      viptype: options.viptype,
      shareid: shareid
    })

    if (options.viptype == 0) {
      wx.setNavigationBarTitle({
        title: '开通会员'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '续费会员'
      })
    }
    that.personalInfo();
    that.vipList();
  },
  //我的个人信息
  personalInfo: function(e) {
    var that = this;
    utils.http("/api/v1/member/index", {}, function(res) {
      that.setData({
        info: res.datas
      })
    })
  },
  //支付方式
  paymentTypeTap: function(e) {
    var id = e.currentTarget.dataset.id;
    var currentBut = id;
    this.setData({
      currentBut: currentBut,
      type: e.currentTarget.dataset.type
    })
  },
  //选择会员套餐
  vipList: function(e) {
    var that = this
    utils.http("/api/v1/member/vip_list", {}, function(res) {
      that.setData({
        vipData: res.datas,
        vipId: res.datas[0].id
      })
    })
  },
  //tab切换
  tab: function(e) {

    var vipId = e.currentTarget.dataset.vipid;
    var dataId = e.currentTarget.id;
    var curHdIndex = dataId;
    this.setData({
      curHdIndex: curHdIndex,
      vipId: vipId
    })
    //console.log(e);
  },
  preventTouchMove() {},
  //打开支付弹窗
  openvipButton: function(e) {
    var that = this;
    that.setData({
      isHide: false
    })
  },

  //关闭支付弹窗
  onColseTap: function(e) {
    var that = this;
    this.setData({
      isHide: true,
      isMove: true
    })
    that.personalInfo();
  },
  //支付成功
  onOrderSuccessTap: function(e) {
    var that = this;
    console.log(that.data.type);
    if (that.data.type == "wx_pay") {
      utils.http("/api/v1/member/add_vip_order", {
        vip_id: that.data.vipId,
        peyment_code: that.data.type,
        share_vip_id: that.data.shareid
      }, function(res) {
        if (res.state == 1) {
          wx.requestPayment({
            timeStamp: res.datas.timeStamp,
            nonceStr: res.datas.nonceStr,
            package: res.datas.package,
            signType: res.datas.signType,
            paySign: res.datas.paySign,
            success(res) {
              that.setData({
                isMove: false
              })
            },
            fail(res) {}
          })
        }
      })
    }
    if (that.data.type == "balance") {
      utils.http("/api/v1/member/add_vip_order", {
        vip_id: that.data.vipId,
        peyment_code: that.data.type,
        share_vip_id: that.data.shareid
      }, function(res) {
        if (res.state == 1) {
          that.setData({
            isMove: false
          })
        } else {
          wx.showToast({
            title: res.datas,
            icon: 'none'
          })
        }
      })
    }
  },
  //去逛逛
  onGogoTap: function(e) {
    wx.reLaunch({
      url: '../vipmember/vipmember',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})